<template>
    <div></div>
</template>

<script>
import {checkBind} from '@/api/login'
import {
    setToken,
    getOAuth2AccessToken,
    getOAuth2RefreshToken,
    setOAuth2AccessToken,
    setOAuth2RefreshToken,
    removeOAuth2RefreshToken,
    removeOAuth2AccessToken
} from '@/utils/auth'
import {loginByCode} from '@/api/oauth2/oauth2'

export default {
    inject: ['reload'],
    name: 'Callback',
    data() {
        return {}
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    created() {
        let code = this.getParam('code')
        if (code){
            loginByCode(code,'http://127.0.0.1:81/agriculture/callback').then(response=>{
                console.log('response',response)
                setOAuth2AccessToken(response.data.access_token)
                setOAuth2RefreshToken(response.data.refresh_token)

                this.$router.push({ path: '/check/bind' }).catch(() => {
                })
            })
        }

    },
    methods: {
        getParam(key) {
            let url = window.location.href
            let params = url.split('?')[1]
            let paramsObj = new URLSearchParams('?' + params)
            console.log('paramsObj', paramsObj)
            let value = paramsObj.get(key)
            console.log('value', value)
            return value
        }
    }
}
</script>

<style rel="stylesheet/scss" lang="scss">
</style>
